package com.chris.libs.example.common.http;

import android.view.View;

import com.chris.dev.base.libs.v1.http.protocols.HttpResult;
import com.chris.libs.base.app.SuccessStatusNetCallback;
import com.chris.libs.http.x.v1.CompleteCallback;
import com.chris.libs.http.x.v1.DefaultCallback;
import com.chris.libs.http.x.v1.NetClient;

import okhttp3.Request;
import retrofit2.Call;

/**
 * @author Chris chan
 * Create on： 2025/2/4 13:21
 * Use for:
 * Explain:
 */
public class DefaultHttp {

    /**
     * 默认返回成功的回调
     *
     * @param call
     * @param callback
     * @param <T>
     */
    public static <T> void post(Call<HttpResult<T>> call, CompleteCallback<T> callback) {
        call.enqueue(new DefaultCallback<>(callback));
    }

    /**
     * 带着Authorization
     * 未实现
     *
     * @param call
     * @param callback
     * @param <T>
     */
    public static <T> void postWithAuth(Call<HttpResult<T>> call, CompleteCallback<T> callback) {
        NetClient.setDefaultInterceptor(chain -> {
            Request request = chain.request()
                    .newBuilder()
                    .addHeader("Authorization", "Bearer thismyauthorizationtokenhahahaha")
                    .build();
            return chain.proceed(request);
        });
        call.enqueue(new DefaultCallback<>(callback));
    }

    /**
     * 带有请求状态的post请求
     *
     * @param call     接口调用
     * @param ownView  需要更新数据的View
     * @param callback 接口调用回调
     * @param <T>      响应数据类型
     */
    public static <T> void post(Call<HttpResult<T>> call, View ownView, CompleteCallback<T> callback) {
        call.enqueue(new SuccessStatusNetCallback<>(callback).with(ownView));
    }

    /**
     * 带着Authorization
     * 未实现
     *
     * @param call
     * @param ownView
     * @param callback
     * @param <T>
     */
    public static <T> void postWithAuth(Call<HttpResult<T>> call, View ownView, CompleteCallback<T> callback) {
        NetClient.setDefaultInterceptor(chain -> {
            Request request = chain.request()
                    .newBuilder()
                    .addHeader("Authorization", "Bearer thismyauthorizationtokenhahahaha")
                    .build();
            return chain.proceed(request);
        });
        call.enqueue(new SuccessStatusNetCallback<>(callback).with(ownView));
    }
}
